package mess;

import java.util.Arrays;

public class LeeCode_1250 {
    public static void main(String[] args) {
        int[] nums = new int[]{1};
        System.out.println(isGoodArray(nums));
    }

    private static boolean isGoodArray(int[] nums) {
        Arrays.sort(nums);
        int x = nums[0];
        int len = nums.length;
        for (int i = 1; i < len; i++) {
            int max = 1;
            //计算x和nums[i]的最大公约数并更新x
            int num = nums[i];
            double middle = Math.sqrt(x);
            for (int j = 1; j <= middle; j++) {
                if (x % j == 0){
                    int temp = x / j;
                    if (num % temp == 0) {
                        max = temp;
                        break;
                    }else if (num % j == 0)
                        max = Math.max(max, j);
                }
            }
            x = max;
            if (x == 1)
                return true;
        }
        return x == 1;
    }
}
